﻿using System.Text.Json;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace RAG.Infrastructure.Migrations
{
    /// <inheritdoc />
    public partial class RAGInit2 : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Conversations_AppUsers_UserId",
                table: "Conversations");

            migrationBuilder.DropForeignKey(
                name: "FK_Conversations_Sessions_SessionId",
                table: "Conversations");

            migrationBuilder.DropForeignKey(
                name: "FK_DocumentChunks_Documents_DocumentId",
                table: "DocumentChunks");

            migrationBuilder.DropForeignKey(
                name: "FK_Documents_AppUsers_UserId",
                table: "Documents");

            migrationBuilder.DropForeignKey(
                name: "FK_Documents_KnowledgeBases_KnowledgeBaseId",
                table: "Documents");

            migrationBuilder.DropForeignKey(
                name: "FK_KnowledgeBases_AppUsers_UserId",
                table: "KnowledgeBases");

            migrationBuilder.DropForeignKey(
                name: "FK_RolePermissions_Permissions_PermissionId",
                table: "RolePermissions");

            migrationBuilder.DropForeignKey(
                name: "FK_RolePermissions_Roles_RoleId",
                table: "RolePermissions");

            migrationBuilder.DropForeignKey(
                name: "FK_Sessions_AppUsers_UserId",
                table: "Sessions");

            migrationBuilder.DropForeignKey(
                name: "FK_UserRoles_AppUsers_UserId",
                table: "UserRoles");

            migrationBuilder.DropForeignKey(
                name: "FK_UserRoles_Roles_RoleId",
                table: "UserRoles");

            migrationBuilder.DropForeignKey(
                name: "FK_Vectors_DocumentChunks_ChunkId",
                table: "Vectors");

            migrationBuilder.DropPrimaryKey(
                name: "PK_Vectors",
                table: "Vectors");

            migrationBuilder.DropPrimaryKey(
                name: "PK_Sessions",
                table: "Sessions");

            migrationBuilder.DropPrimaryKey(
                name: "PK_Roles",
                table: "Roles");

            migrationBuilder.DropPrimaryKey(
                name: "PK_Permissions",
                table: "Permissions");

            migrationBuilder.DropIndex(
                name: "IX_Permissions_Code",
                table: "Permissions");

            migrationBuilder.DropPrimaryKey(
                name: "PK_Documents",
                table: "Documents");

            migrationBuilder.DropPrimaryKey(
                name: "PK_Conversations",
                table: "Conversations");

            migrationBuilder.DropPrimaryKey(
                name: "PK_UserRoles",
                table: "UserRoles");

            migrationBuilder.DropPrimaryKey(
                name: "PK_RolePermissions",
                table: "RolePermissions");

            migrationBuilder.DropPrimaryKey(
                name: "PK_KnowledgeBases",
                table: "KnowledgeBases");

            migrationBuilder.DropPrimaryKey(
                name: "PK_DocumentChunks",
                table: "DocumentChunks");

            migrationBuilder.DropPrimaryKey(
                name: "PK_AppUsers",
                table: "AppUsers");

            migrationBuilder.DropColumn(
                name: "Status",
                table: "Sessions");

            migrationBuilder.DropColumn(
                name: "Title",
                table: "Sessions");

            migrationBuilder.DropColumn(
                name: "Description",
                table: "Roles");

            migrationBuilder.DropColumn(
                name: "Code",
                table: "Permissions");

            migrationBuilder.DropColumn(
                name: "Description",
                table: "Permissions");

            migrationBuilder.DropColumn(
                name: "AccessLevel",
                table: "Documents");

            migrationBuilder.DropColumn(
                name: "FileName",
                table: "Documents");

            migrationBuilder.DropColumn(
                name: "Status",
                table: "Documents");

            migrationBuilder.DropColumn(
                name: "SourceChunks",
                table: "Conversations");

            migrationBuilder.DropColumn(
                name: "Description",
                table: "KnowledgeBases");

            migrationBuilder.DropColumn(
                name: "Name",
                table: "KnowledgeBases");

            migrationBuilder.RenameTable(
                name: "Vectors",
                newName: "vectors");

            migrationBuilder.RenameTable(
                name: "Sessions",
                newName: "sessions");

            migrationBuilder.RenameTable(
                name: "Roles",
                newName: "roles");

            migrationBuilder.RenameTable(
                name: "Permissions",
                newName: "permissions");

            migrationBuilder.RenameTable(
                name: "Documents",
                newName: "documents");

            migrationBuilder.RenameTable(
                name: "Conversations",
                newName: "conversations");

            migrationBuilder.RenameTable(
                name: "UserRoles",
                newName: "user_roles");

            migrationBuilder.RenameTable(
                name: "RolePermissions",
                newName: "role_permissions");

            migrationBuilder.RenameTable(
                name: "KnowledgeBases",
                newName: "knowledge_bases");

            migrationBuilder.RenameTable(
                name: "DocumentChunks",
                newName: "document_chunks");

            migrationBuilder.RenameTable(
                name: "AppUsers",
                newName: "app_users");

            migrationBuilder.RenameColumn(
                name: "Embedding",
                table: "vectors",
                newName: "embedding");

            migrationBuilder.RenameColumn(
                name: "UpdatedAt",
                table: "vectors",
                newName: "updated_at");

            migrationBuilder.RenameColumn(
                name: "CreatedAt",
                table: "vectors",
                newName: "created_at");

            migrationBuilder.RenameColumn(
                name: "ChunkId",
                table: "vectors",
                newName: "vector_chunk_id");

            migrationBuilder.RenameColumn(
                name: "Id",
                table: "vectors",
                newName: "vector_id");

            migrationBuilder.RenameIndex(
                name: "IX_Vectors_ChunkId",
                table: "vectors",
                newName: "IX_vectors_vector_chunk_id");

            migrationBuilder.RenameColumn(
                name: "Metadata",
                table: "sessions",
                newName: "metadata");

            migrationBuilder.RenameColumn(
                name: "Id",
                table: "sessions",
                newName: "id");

            migrationBuilder.RenameColumn(
                name: "UserId",
                table: "sessions",
                newName: "user_id");

            migrationBuilder.RenameColumn(
                name: "UpdatedAt",
                table: "sessions",
                newName: "updated_at");

            migrationBuilder.RenameColumn(
                name: "LastActiveAt",
                table: "sessions",
                newName: "last_active_at");

            migrationBuilder.RenameColumn(
                name: "CreatedAt",
                table: "sessions",
                newName: "created_at");

            migrationBuilder.RenameIndex(
                name: "IX_Sessions_UserId",
                table: "sessions",
                newName: "IX_sessions_user_id");

            migrationBuilder.RenameColumn(
                name: "Id",
                table: "roles",
                newName: "id");

            migrationBuilder.RenameColumn(
                name: "UpdatedAt",
                table: "roles",
                newName: "updated_at");

            migrationBuilder.RenameColumn(
                name: "CreatedAt",
                table: "roles",
                newName: "created_at");

            migrationBuilder.RenameColumn(
                name: "Name",
                table: "roles",
                newName: "role_name");

            migrationBuilder.RenameIndex(
                name: "IX_Roles_Name",
                table: "roles",
                newName: "IX_roles_role_name");

            migrationBuilder.RenameColumn(
                name: "Id",
                table: "permissions",
                newName: "id");

            migrationBuilder.RenameColumn(
                name: "UpdatedAt",
                table: "permissions",
                newName: "updated_at");

            migrationBuilder.RenameColumn(
                name: "CreatedAt",
                table: "permissions",
                newName: "created_at");

            migrationBuilder.RenameColumn(
                name: "Name",
                table: "permissions",
                newName: "permission_name");

            migrationBuilder.RenameColumn(
                name: "UserId",
                table: "documents",
                newName: "user_id");

            migrationBuilder.RenameColumn(
                name: "UpdatedAt",
                table: "documents",
                newName: "updated_at");

            migrationBuilder.RenameColumn(
                name: "KnowledgeBaseId",
                table: "documents",
                newName: "knowledge_base_id");

            migrationBuilder.RenameColumn(
                name: "CreatedAt",
                table: "documents",
                newName: "created_at");

            migrationBuilder.RenameColumn(
                name: "Title",
                table: "documents",
                newName: "document_file_name");

            migrationBuilder.RenameColumn(
                name: "FileType",
                table: "documents",
                newName: "document_status");

            migrationBuilder.RenameColumn(
                name: "FileSize",
                table: "documents",
                newName: "document_file_size");

            migrationBuilder.RenameColumn(
                name: "FilePath",
                table: "documents",
                newName: "document_file_path");

            migrationBuilder.RenameColumn(
                name: "Id",
                table: "documents",
                newName: "document_id");

            migrationBuilder.RenameIndex(
                name: "IX_Documents_UserId",
                table: "documents",
                newName: "IX_documents_user_id");

            migrationBuilder.RenameIndex(
                name: "IX_Documents_KnowledgeBaseId",
                table: "documents",
                newName: "IX_documents_knowledge_base_id");

            migrationBuilder.RenameColumn(
                name: "Id",
                table: "conversations",
                newName: "id");

            migrationBuilder.RenameColumn(
                name: "UserId",
                table: "conversations",
                newName: "user_id");

            migrationBuilder.RenameColumn(
                name: "UpdatedAt",
                table: "conversations",
                newName: "updated_at");

            migrationBuilder.RenameColumn(
                name: "SessionId",
                table: "conversations",
                newName: "session_id");

            migrationBuilder.RenameColumn(
                name: "PromptTokens",
                table: "conversations",
                newName: "prompt_tokens");

            migrationBuilder.RenameColumn(
                name: "LatencyMs",
                table: "conversations",
                newName: "latency_ms");

            migrationBuilder.RenameColumn(
                name: "CreatedAt",
                table: "conversations",
                newName: "created_at");

            migrationBuilder.RenameColumn(
                name: "CompletionTokens",
                table: "conversations",
                newName: "completion_tokens");

            migrationBuilder.RenameColumn(
                name: "Question",
                table: "conversations",
                newName: "conversation_question");

            migrationBuilder.RenameColumn(
                name: "Model",
                table: "conversations",
                newName: "conversation_model");

            migrationBuilder.RenameColumn(
                name: "Answer",
                table: "conversations",
                newName: "conversation_answer");

            migrationBuilder.RenameIndex(
                name: "IX_Conversations_UserId",
                table: "conversations",
                newName: "IX_conversations_user_id");

            migrationBuilder.RenameIndex(
                name: "IX_Conversations_SessionId",
                table: "conversations",
                newName: "IX_conversations_session_id");

            migrationBuilder.RenameColumn(
                name: "UpdatedAt",
                table: "user_roles",
                newName: "updated_at");

            migrationBuilder.RenameColumn(
                name: "CreatedAt",
                table: "user_roles",
                newName: "created_at");

            migrationBuilder.RenameColumn(
                name: "RoleId",
                table: "user_roles",
                newName: "role_id");

            migrationBuilder.RenameColumn(
                name: "UserId",
                table: "user_roles",
                newName: "user_id");

            migrationBuilder.RenameIndex(
                name: "IX_UserRoles_RoleId",
                table: "user_roles",
                newName: "IX_user_roles_role_id");

            migrationBuilder.RenameColumn(
                name: "UpdatedAt",
                table: "role_permissions",
                newName: "updated_at");

            migrationBuilder.RenameColumn(
                name: "CreatedAt",
                table: "role_permissions",
                newName: "created_at");

            migrationBuilder.RenameColumn(
                name: "PermissionId",
                table: "role_permissions",
                newName: "permission_id");

            migrationBuilder.RenameColumn(
                name: "RoleId",
                table: "role_permissions",
                newName: "role_id");

            migrationBuilder.RenameIndex(
                name: "IX_RolePermissions_PermissionId",
                table: "role_permissions",
                newName: "IX_role_permissions_permission_id");

            migrationBuilder.RenameColumn(
                name: "UserId",
                table: "knowledge_bases",
                newName: "user_id");

            migrationBuilder.RenameColumn(
                name: "UpdatedAt",
                table: "knowledge_bases",
                newName: "updated_at");

            migrationBuilder.RenameColumn(
                name: "IsPublic",
                table: "knowledge_bases",
                newName: "is_public");

            migrationBuilder.RenameColumn(
                name: "CreatedAt",
                table: "knowledge_bases",
                newName: "created_at");

            migrationBuilder.RenameColumn(
                name: "Id",
                table: "knowledge_bases",
                newName: "knowledge_base_id");

            migrationBuilder.RenameIndex(
                name: "IX_KnowledgeBases_UserId",
                table: "knowledge_bases",
                newName: "IX_knowledge_bases_user_id");

            migrationBuilder.RenameColumn(
                name: "UpdatedAt",
                table: "document_chunks",
                newName: "updated_at");

            migrationBuilder.RenameColumn(
                name: "CreatedAt",
                table: "document_chunks",
                newName: "created_at");

            migrationBuilder.RenameColumn(
                name: "TokenCount",
                table: "document_chunks",
                newName: "document_chunk_token_count");

            migrationBuilder.RenameColumn(
                name: "DocumentId",
                table: "document_chunks",
                newName: "document_chunk_document_id");

            migrationBuilder.RenameColumn(
                name: "Content",
                table: "document_chunks",
                newName: "document_chunk_content");

            migrationBuilder.RenameColumn(
                name: "ChunkIndex",
                table: "document_chunks",
                newName: "document_chunk_chunk_index");

            migrationBuilder.RenameColumn(
                name: "Id",
                table: "document_chunks",
                newName: "document_chunk_id");

            migrationBuilder.RenameIndex(
                name: "IX_DocumentChunks_DocumentId",
                table: "document_chunks",
                newName: "IX_document_chunks_document_chunk_document_id");

            migrationBuilder.RenameColumn(
                name: "Username",
                table: "app_users",
                newName: "username");

            migrationBuilder.RenameColumn(
                name: "Salt",
                table: "app_users",
                newName: "salt");

            migrationBuilder.RenameColumn(
                name: "Phone",
                table: "app_users",
                newName: "phone");

            migrationBuilder.RenameColumn(
                name: "Password",
                table: "app_users",
                newName: "password");

            migrationBuilder.RenameColumn(
                name: "Email",
                table: "app_users",
                newName: "email");

            migrationBuilder.RenameColumn(
                name: "Avatar",
                table: "app_users",
                newName: "avatar");

            migrationBuilder.RenameColumn(
                name: "Id",
                table: "app_users",
                newName: "id");

            migrationBuilder.RenameColumn(
                name: "UpdatedAt",
                table: "app_users",
                newName: "updated_at");

            migrationBuilder.RenameColumn(
                name: "LockedUntil",
                table: "app_users",
                newName: "locked_until");

            migrationBuilder.RenameColumn(
                name: "LastLoginTime",
                table: "app_users",
                newName: "last_login_time");

            migrationBuilder.RenameColumn(
                name: "LastFailedLogin",
                table: "app_users",
                newName: "last_failed_login");

            migrationBuilder.RenameColumn(
                name: "LastActivityTime",
                table: "app_users",
                newName: "last_activity_time");

            migrationBuilder.RenameColumn(
                name: "FailedLoginAttempts",
                table: "app_users",
                newName: "failed_login_attempts");

            migrationBuilder.RenameColumn(
                name: "CreatedAt",
                table: "app_users",
                newName: "created_at");

            migrationBuilder.RenameColumn(
                name: "AccountStatus",
                table: "app_users",
                newName: "account_status");

            migrationBuilder.RenameIndex(
                name: "IX_AppUsers_Username",
                table: "app_users",
                newName: "IX_app_users_username");

            migrationBuilder.RenameIndex(
                name: "IX_AppUsers_Email",
                table: "app_users",
                newName: "IX_app_users_email");

            migrationBuilder.AddColumn<string>(
                name: "session_status",
                table: "sessions",
                type: "character varying(50)",
                maxLength: 50,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<string>(
                name: "session_title",
                table: "sessions",
                type: "character varying(200)",
                maxLength: 200,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<string>(
                name: "role_description",
                table: "roles",
                type: "character varying(200)",
                maxLength: 200,
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "permission_code",
                table: "permissions",
                type: "character varying(100)",
                maxLength: 100,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<string>(
                name: "permission_description",
                table: "permissions",
                type: "character varying(200)",
                maxLength: 200,
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "document_access_level",
                table: "documents",
                type: "character varying(50)",
                maxLength: 50,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<string>(
                name: "document_file_type",
                table: "documents",
                type: "character varying(50)",
                maxLength: 50,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<string>(
                name: "document_title",
                table: "documents",
                type: "character varying(200)",
                maxLength: 200,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<JsonDocument>(
                name: "conversation_source_chunks",
                table: "conversations",
                type: "jsonb",
                nullable: true);

            migrationBuilder.AlterColumn<bool>(
                name: "is_public",
                table: "knowledge_bases",
                type: "boolean",
                nullable: false,
                defaultValue: false,
                oldClrType: typeof(bool),
                oldType: "boolean");

            migrationBuilder.AddColumn<string>(
                name: "knowledge_base_description",
                table: "knowledge_bases",
                type: "character varying(500)",
                maxLength: 500,
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "knowledge_base_name",
                table: "knowledge_bases",
                type: "character varying(100)",
                maxLength: 100,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AlterColumn<string>(
                name: "password",
                table: "app_users",
                type: "character varying(255)",
                maxLength: 255,
                nullable: false,
                oldClrType: typeof(string),
                oldType: "character varying(100)",
                oldMaxLength: 100);

            migrationBuilder.AlterColumn<string>(
                name: "avatar",
                table: "app_users",
                type: "character varying(500)",
                maxLength: 500,
                nullable: true,
                oldClrType: typeof(string),
                oldType: "character varying(255)",
                oldMaxLength: 255,
                oldNullable: true);

            migrationBuilder.AlterColumn<int>(
                name: "failed_login_attempts",
                table: "app_users",
                type: "integer",
                nullable: false,
                defaultValue: 0,
                oldClrType: typeof(int),
                oldType: "integer");

            migrationBuilder.AlterColumn<int>(
                name: "account_status",
                table: "app_users",
                type: "integer",
                nullable: false,
                defaultValue: 0,
                oldClrType: typeof(int),
                oldType: "integer");

            migrationBuilder.AddPrimaryKey(
                name: "PK_vectors",
                table: "vectors",
                column: "vector_id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_sessions",
                table: "sessions",
                column: "id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_roles",
                table: "roles",
                column: "id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_permissions",
                table: "permissions",
                column: "id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_documents",
                table: "documents",
                column: "document_id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_conversations",
                table: "conversations",
                column: "id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_user_roles",
                table: "user_roles",
                columns: new[] { "user_id", "role_id" });

            migrationBuilder.AddPrimaryKey(
                name: "PK_role_permissions",
                table: "role_permissions",
                columns: new[] { "role_id", "permission_id" });

            migrationBuilder.AddPrimaryKey(
                name: "PK_knowledge_bases",
                table: "knowledge_bases",
                column: "knowledge_base_id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_document_chunks",
                table: "document_chunks",
                column: "document_chunk_id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_app_users",
                table: "app_users",
                column: "id");

            migrationBuilder.CreateIndex(
                name: "IX_sessions_last_active_at",
                table: "sessions",
                column: "last_active_at");

            migrationBuilder.CreateIndex(
                name: "IX_sessions_session_status",
                table: "sessions",
                column: "session_status");

            migrationBuilder.CreateIndex(
                name: "IX_permissions_permission_code",
                table: "permissions",
                column: "permission_code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_documents_document_status",
                table: "documents",
                column: "document_status");

            migrationBuilder.CreateIndex(
                name: "IX_conversations_created_at",
                table: "conversations",
                column: "created_at");

            migrationBuilder.CreateIndex(
                name: "IX_user_roles_user_id",
                table: "user_roles",
                column: "user_id");

            migrationBuilder.CreateIndex(
                name: "IX_role_permissions_role_id",
                table: "role_permissions",
                column: "role_id");

            migrationBuilder.CreateIndex(
                name: "IX_knowledge_bases_is_public",
                table: "knowledge_bases",
                column: "is_public");

            migrationBuilder.CreateIndex(
                name: "IX_document_chunks_document_chunk_chunk_index",
                table: "document_chunks",
                column: "document_chunk_chunk_index");

            migrationBuilder.CreateIndex(
                name: "IX_app_users_phone",
                table: "app_users",
                column: "phone");

            migrationBuilder.AddForeignKey(
                name: "FK_conversations_app_users_user_id",
                table: "conversations",
                column: "user_id",
                principalTable: "app_users",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_conversations_sessions_session_id",
                table: "conversations",
                column: "session_id",
                principalTable: "sessions",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_document_chunks_documents_document_chunk_document_id",
                table: "document_chunks",
                column: "document_chunk_document_id",
                principalTable: "documents",
                principalColumn: "document_id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_documents_app_users_user_id",
                table: "documents",
                column: "user_id",
                principalTable: "app_users",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_documents_knowledge_bases_knowledge_base_id",
                table: "documents",
                column: "knowledge_base_id",
                principalTable: "knowledge_bases",
                principalColumn: "knowledge_base_id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_knowledge_bases_app_users_user_id",
                table: "knowledge_bases",
                column: "user_id",
                principalTable: "app_users",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_role_permissions_permissions_permission_id",
                table: "role_permissions",
                column: "permission_id",
                principalTable: "permissions",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_role_permissions_roles_role_id",
                table: "role_permissions",
                column: "role_id",
                principalTable: "roles",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_sessions_app_users_user_id",
                table: "sessions",
                column: "user_id",
                principalTable: "app_users",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_user_roles_app_users_user_id",
                table: "user_roles",
                column: "user_id",
                principalTable: "app_users",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_user_roles_roles_role_id",
                table: "user_roles",
                column: "role_id",
                principalTable: "roles",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_vectors_document_chunks_vector_chunk_id",
                table: "vectors",
                column: "vector_chunk_id",
                principalTable: "document_chunks",
                principalColumn: "document_chunk_id",
                onDelete: ReferentialAction.Cascade);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_conversations_app_users_user_id",
                table: "conversations");

            migrationBuilder.DropForeignKey(
                name: "FK_conversations_sessions_session_id",
                table: "conversations");

            migrationBuilder.DropForeignKey(
                name: "FK_document_chunks_documents_document_chunk_document_id",
                table: "document_chunks");

            migrationBuilder.DropForeignKey(
                name: "FK_documents_app_users_user_id",
                table: "documents");

            migrationBuilder.DropForeignKey(
                name: "FK_documents_knowledge_bases_knowledge_base_id",
                table: "documents");

            migrationBuilder.DropForeignKey(
                name: "FK_knowledge_bases_app_users_user_id",
                table: "knowledge_bases");

            migrationBuilder.DropForeignKey(
                name: "FK_role_permissions_permissions_permission_id",
                table: "role_permissions");

            migrationBuilder.DropForeignKey(
                name: "FK_role_permissions_roles_role_id",
                table: "role_permissions");

            migrationBuilder.DropForeignKey(
                name: "FK_sessions_app_users_user_id",
                table: "sessions");

            migrationBuilder.DropForeignKey(
                name: "FK_user_roles_app_users_user_id",
                table: "user_roles");

            migrationBuilder.DropForeignKey(
                name: "FK_user_roles_roles_role_id",
                table: "user_roles");

            migrationBuilder.DropForeignKey(
                name: "FK_vectors_document_chunks_vector_chunk_id",
                table: "vectors");

            migrationBuilder.DropPrimaryKey(
                name: "PK_vectors",
                table: "vectors");

            migrationBuilder.DropPrimaryKey(
                name: "PK_sessions",
                table: "sessions");

            migrationBuilder.DropIndex(
                name: "IX_sessions_last_active_at",
                table: "sessions");

            migrationBuilder.DropIndex(
                name: "IX_sessions_session_status",
                table: "sessions");

            migrationBuilder.DropPrimaryKey(
                name: "PK_roles",
                table: "roles");

            migrationBuilder.DropPrimaryKey(
                name: "PK_permissions",
                table: "permissions");

            migrationBuilder.DropIndex(
                name: "IX_permissions_permission_code",
                table: "permissions");

            migrationBuilder.DropPrimaryKey(
                name: "PK_documents",
                table: "documents");

            migrationBuilder.DropIndex(
                name: "IX_documents_document_status",
                table: "documents");

            migrationBuilder.DropPrimaryKey(
                name: "PK_conversations",
                table: "conversations");

            migrationBuilder.DropIndex(
                name: "IX_conversations_created_at",
                table: "conversations");

            migrationBuilder.DropPrimaryKey(
                name: "PK_user_roles",
                table: "user_roles");

            migrationBuilder.DropIndex(
                name: "IX_user_roles_user_id",
                table: "user_roles");

            migrationBuilder.DropPrimaryKey(
                name: "PK_role_permissions",
                table: "role_permissions");

            migrationBuilder.DropIndex(
                name: "IX_role_permissions_role_id",
                table: "role_permissions");

            migrationBuilder.DropPrimaryKey(
                name: "PK_knowledge_bases",
                table: "knowledge_bases");

            migrationBuilder.DropIndex(
                name: "IX_knowledge_bases_is_public",
                table: "knowledge_bases");

            migrationBuilder.DropPrimaryKey(
                name: "PK_document_chunks",
                table: "document_chunks");

            migrationBuilder.DropIndex(
                name: "IX_document_chunks_document_chunk_chunk_index",
                table: "document_chunks");

            migrationBuilder.DropPrimaryKey(
                name: "PK_app_users",
                table: "app_users");

            migrationBuilder.DropIndex(
                name: "IX_app_users_phone",
                table: "app_users");

            migrationBuilder.DropColumn(
                name: "session_status",
                table: "sessions");

            migrationBuilder.DropColumn(
                name: "session_title",
                table: "sessions");

            migrationBuilder.DropColumn(
                name: "role_description",
                table: "roles");

            migrationBuilder.DropColumn(
                name: "permission_code",
                table: "permissions");

            migrationBuilder.DropColumn(
                name: "permission_description",
                table: "permissions");

            migrationBuilder.DropColumn(
                name: "document_access_level",
                table: "documents");

            migrationBuilder.DropColumn(
                name: "document_file_type",
                table: "documents");

            migrationBuilder.DropColumn(
                name: "document_title",
                table: "documents");

            migrationBuilder.DropColumn(
                name: "conversation_source_chunks",
                table: "conversations");

            migrationBuilder.DropColumn(
                name: "knowledge_base_description",
                table: "knowledge_bases");

            migrationBuilder.DropColumn(
                name: "knowledge_base_name",
                table: "knowledge_bases");

            migrationBuilder.RenameTable(
                name: "vectors",
                newName: "Vectors");

            migrationBuilder.RenameTable(
                name: "sessions",
                newName: "Sessions");

            migrationBuilder.RenameTable(
                name: "roles",
                newName: "Roles");

            migrationBuilder.RenameTable(
                name: "permissions",
                newName: "Permissions");

            migrationBuilder.RenameTable(
                name: "documents",
                newName: "Documents");

            migrationBuilder.RenameTable(
                name: "conversations",
                newName: "Conversations");

            migrationBuilder.RenameTable(
                name: "user_roles",
                newName: "UserRoles");

            migrationBuilder.RenameTable(
                name: "role_permissions",
                newName: "RolePermissions");

            migrationBuilder.RenameTable(
                name: "knowledge_bases",
                newName: "KnowledgeBases");

            migrationBuilder.RenameTable(
                name: "document_chunks",
                newName: "DocumentChunks");

            migrationBuilder.RenameTable(
                name: "app_users",
                newName: "AppUsers");

            migrationBuilder.RenameColumn(
                name: "embedding",
                table: "Vectors",
                newName: "Embedding");

            migrationBuilder.RenameColumn(
                name: "updated_at",
                table: "Vectors",
                newName: "UpdatedAt");

            migrationBuilder.RenameColumn(
                name: "created_at",
                table: "Vectors",
                newName: "CreatedAt");

            migrationBuilder.RenameColumn(
                name: "vector_chunk_id",
                table: "Vectors",
                newName: "ChunkId");

            migrationBuilder.RenameColumn(
                name: "vector_id",
                table: "Vectors",
                newName: "Id");

            migrationBuilder.RenameIndex(
                name: "IX_vectors_vector_chunk_id",
                table: "Vectors",
                newName: "IX_Vectors_ChunkId");

            migrationBuilder.RenameColumn(
                name: "metadata",
                table: "Sessions",
                newName: "Metadata");

            migrationBuilder.RenameColumn(
                name: "id",
                table: "Sessions",
                newName: "Id");

            migrationBuilder.RenameColumn(
                name: "user_id",
                table: "Sessions",
                newName: "UserId");

            migrationBuilder.RenameColumn(
                name: "updated_at",
                table: "Sessions",
                newName: "UpdatedAt");

            migrationBuilder.RenameColumn(
                name: "last_active_at",
                table: "Sessions",
                newName: "LastActiveAt");

            migrationBuilder.RenameColumn(
                name: "created_at",
                table: "Sessions",
                newName: "CreatedAt");

            migrationBuilder.RenameIndex(
                name: "IX_sessions_user_id",
                table: "Sessions",
                newName: "IX_Sessions_UserId");

            migrationBuilder.RenameColumn(
                name: "id",
                table: "Roles",
                newName: "Id");

            migrationBuilder.RenameColumn(
                name: "updated_at",
                table: "Roles",
                newName: "UpdatedAt");

            migrationBuilder.RenameColumn(
                name: "created_at",
                table: "Roles",
                newName: "CreatedAt");

            migrationBuilder.RenameColumn(
                name: "role_name",
                table: "Roles",
                newName: "Name");

            migrationBuilder.RenameIndex(
                name: "IX_roles_role_name",
                table: "Roles",
                newName: "IX_Roles_Name");

            migrationBuilder.RenameColumn(
                name: "id",
                table: "Permissions",
                newName: "Id");

            migrationBuilder.RenameColumn(
                name: "updated_at",
                table: "Permissions",
                newName: "UpdatedAt");

            migrationBuilder.RenameColumn(
                name: "created_at",
                table: "Permissions",
                newName: "CreatedAt");

            migrationBuilder.RenameColumn(
                name: "permission_name",
                table: "Permissions",
                newName: "Name");

            migrationBuilder.RenameColumn(
                name: "user_id",
                table: "Documents",
                newName: "UserId");

            migrationBuilder.RenameColumn(
                name: "updated_at",
                table: "Documents",
                newName: "UpdatedAt");

            migrationBuilder.RenameColumn(
                name: "knowledge_base_id",
                table: "Documents",
                newName: "KnowledgeBaseId");

            migrationBuilder.RenameColumn(
                name: "created_at",
                table: "Documents",
                newName: "CreatedAt");

            migrationBuilder.RenameColumn(
                name: "document_status",
                table: "Documents",
                newName: "FileType");

            migrationBuilder.RenameColumn(
                name: "document_file_size",
                table: "Documents",
                newName: "FileSize");

            migrationBuilder.RenameColumn(
                name: "document_file_path",
                table: "Documents",
                newName: "FilePath");

            migrationBuilder.RenameColumn(
                name: "document_file_name",
                table: "Documents",
                newName: "Title");

            migrationBuilder.RenameColumn(
                name: "document_id",
                table: "Documents",
                newName: "Id");

            migrationBuilder.RenameIndex(
                name: "IX_documents_user_id",
                table: "Documents",
                newName: "IX_Documents_UserId");

            migrationBuilder.RenameIndex(
                name: "IX_documents_knowledge_base_id",
                table: "Documents",
                newName: "IX_Documents_KnowledgeBaseId");

            migrationBuilder.RenameColumn(
                name: "id",
                table: "Conversations",
                newName: "Id");

            migrationBuilder.RenameColumn(
                name: "user_id",
                table: "Conversations",
                newName: "UserId");

            migrationBuilder.RenameColumn(
                name: "updated_at",
                table: "Conversations",
                newName: "UpdatedAt");

            migrationBuilder.RenameColumn(
                name: "session_id",
                table: "Conversations",
                newName: "SessionId");

            migrationBuilder.RenameColumn(
                name: "prompt_tokens",
                table: "Conversations",
                newName: "PromptTokens");

            migrationBuilder.RenameColumn(
                name: "latency_ms",
                table: "Conversations",
                newName: "LatencyMs");

            migrationBuilder.RenameColumn(
                name: "created_at",
                table: "Conversations",
                newName: "CreatedAt");

            migrationBuilder.RenameColumn(
                name: "completion_tokens",
                table: "Conversations",
                newName: "CompletionTokens");

            migrationBuilder.RenameColumn(
                name: "conversation_question",
                table: "Conversations",
                newName: "Question");

            migrationBuilder.RenameColumn(
                name: "conversation_model",
                table: "Conversations",
                newName: "Model");

            migrationBuilder.RenameColumn(
                name: "conversation_answer",
                table: "Conversations",
                newName: "Answer");

            migrationBuilder.RenameIndex(
                name: "IX_conversations_user_id",
                table: "Conversations",
                newName: "IX_Conversations_UserId");

            migrationBuilder.RenameIndex(
                name: "IX_conversations_session_id",
                table: "Conversations",
                newName: "IX_Conversations_SessionId");

            migrationBuilder.RenameColumn(
                name: "updated_at",
                table: "UserRoles",
                newName: "UpdatedAt");

            migrationBuilder.RenameColumn(
                name: "created_at",
                table: "UserRoles",
                newName: "CreatedAt");

            migrationBuilder.RenameColumn(
                name: "role_id",
                table: "UserRoles",
                newName: "RoleId");

            migrationBuilder.RenameColumn(
                name: "user_id",
                table: "UserRoles",
                newName: "UserId");

            migrationBuilder.RenameIndex(
                name: "IX_user_roles_role_id",
                table: "UserRoles",
                newName: "IX_UserRoles_RoleId");

            migrationBuilder.RenameColumn(
                name: "updated_at",
                table: "RolePermissions",
                newName: "UpdatedAt");

            migrationBuilder.RenameColumn(
                name: "created_at",
                table: "RolePermissions",
                newName: "CreatedAt");

            migrationBuilder.RenameColumn(
                name: "permission_id",
                table: "RolePermissions",
                newName: "PermissionId");

            migrationBuilder.RenameColumn(
                name: "role_id",
                table: "RolePermissions",
                newName: "RoleId");

            migrationBuilder.RenameIndex(
                name: "IX_role_permissions_permission_id",
                table: "RolePermissions",
                newName: "IX_RolePermissions_PermissionId");

            migrationBuilder.RenameColumn(
                name: "user_id",
                table: "KnowledgeBases",
                newName: "UserId");

            migrationBuilder.RenameColumn(
                name: "updated_at",
                table: "KnowledgeBases",
                newName: "UpdatedAt");

            migrationBuilder.RenameColumn(
                name: "is_public",
                table: "KnowledgeBases",
                newName: "IsPublic");

            migrationBuilder.RenameColumn(
                name: "created_at",
                table: "KnowledgeBases",
                newName: "CreatedAt");

            migrationBuilder.RenameColumn(
                name: "knowledge_base_id",
                table: "KnowledgeBases",
                newName: "Id");

            migrationBuilder.RenameIndex(
                name: "IX_knowledge_bases_user_id",
                table: "KnowledgeBases",
                newName: "IX_KnowledgeBases_UserId");

            migrationBuilder.RenameColumn(
                name: "updated_at",
                table: "DocumentChunks",
                newName: "UpdatedAt");

            migrationBuilder.RenameColumn(
                name: "created_at",
                table: "DocumentChunks",
                newName: "CreatedAt");

            migrationBuilder.RenameColumn(
                name: "document_chunk_token_count",
                table: "DocumentChunks",
                newName: "TokenCount");

            migrationBuilder.RenameColumn(
                name: "document_chunk_document_id",
                table: "DocumentChunks",
                newName: "DocumentId");

            migrationBuilder.RenameColumn(
                name: "document_chunk_content",
                table: "DocumentChunks",
                newName: "Content");

            migrationBuilder.RenameColumn(
                name: "document_chunk_chunk_index",
                table: "DocumentChunks",
                newName: "ChunkIndex");

            migrationBuilder.RenameColumn(
                name: "document_chunk_id",
                table: "DocumentChunks",
                newName: "Id");

            migrationBuilder.RenameIndex(
                name: "IX_document_chunks_document_chunk_document_id",
                table: "DocumentChunks",
                newName: "IX_DocumentChunks_DocumentId");

            migrationBuilder.RenameColumn(
                name: "username",
                table: "AppUsers",
                newName: "Username");

            migrationBuilder.RenameColumn(
                name: "salt",
                table: "AppUsers",
                newName: "Salt");

            migrationBuilder.RenameColumn(
                name: "phone",
                table: "AppUsers",
                newName: "Phone");

            migrationBuilder.RenameColumn(
                name: "password",
                table: "AppUsers",
                newName: "Password");

            migrationBuilder.RenameColumn(
                name: "email",
                table: "AppUsers",
                newName: "Email");

            migrationBuilder.RenameColumn(
                name: "avatar",
                table: "AppUsers",
                newName: "Avatar");

            migrationBuilder.RenameColumn(
                name: "id",
                table: "AppUsers",
                newName: "Id");

            migrationBuilder.RenameColumn(
                name: "updated_at",
                table: "AppUsers",
                newName: "UpdatedAt");

            migrationBuilder.RenameColumn(
                name: "locked_until",
                table: "AppUsers",
                newName: "LockedUntil");

            migrationBuilder.RenameColumn(
                name: "last_login_time",
                table: "AppUsers",
                newName: "LastLoginTime");

            migrationBuilder.RenameColumn(
                name: "last_failed_login",
                table: "AppUsers",
                newName: "LastFailedLogin");

            migrationBuilder.RenameColumn(
                name: "last_activity_time",
                table: "AppUsers",
                newName: "LastActivityTime");

            migrationBuilder.RenameColumn(
                name: "failed_login_attempts",
                table: "AppUsers",
                newName: "FailedLoginAttempts");

            migrationBuilder.RenameColumn(
                name: "created_at",
                table: "AppUsers",
                newName: "CreatedAt");

            migrationBuilder.RenameColumn(
                name: "account_status",
                table: "AppUsers",
                newName: "AccountStatus");

            migrationBuilder.RenameIndex(
                name: "IX_app_users_username",
                table: "AppUsers",
                newName: "IX_AppUsers_Username");

            migrationBuilder.RenameIndex(
                name: "IX_app_users_email",
                table: "AppUsers",
                newName: "IX_AppUsers_Email");

            migrationBuilder.AddColumn<string>(
                name: "Status",
                table: "Sessions",
                type: "character varying(20)",
                maxLength: 20,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<string>(
                name: "Title",
                table: "Sessions",
                type: "character varying(255)",
                maxLength: 255,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<string>(
                name: "Description",
                table: "Roles",
                type: "text",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "Code",
                table: "Permissions",
                type: "character varying(50)",
                maxLength: 50,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<string>(
                name: "Description",
                table: "Permissions",
                type: "text",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "AccessLevel",
                table: "Documents",
                type: "character varying(20)",
                maxLength: 20,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<string>(
                name: "FileName",
                table: "Documents",
                type: "character varying(255)",
                maxLength: 255,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<string>(
                name: "Status",
                table: "Documents",
                type: "character varying(20)",
                maxLength: 20,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<JsonDocument>(
                name: "SourceChunks",
                table: "Conversations",
                type: "json",
                nullable: true);

            migrationBuilder.AlterColumn<bool>(
                name: "IsPublic",
                table: "KnowledgeBases",
                type: "boolean",
                nullable: false,
                oldClrType: typeof(bool),
                oldType: "boolean",
                oldDefaultValue: false);

            migrationBuilder.AddColumn<string>(
                name: "Description",
                table: "KnowledgeBases",
                type: "text",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "Name",
                table: "KnowledgeBases",
                type: "character varying(255)",
                maxLength: 255,
                nullable: false,
                defaultValue: "");

            migrationBuilder.AlterColumn<string>(
                name: "Password",
                table: "AppUsers",
                type: "character varying(100)",
                maxLength: 100,
                nullable: false,
                oldClrType: typeof(string),
                oldType: "character varying(255)",
                oldMaxLength: 255);

            migrationBuilder.AlterColumn<string>(
                name: "Avatar",
                table: "AppUsers",
                type: "character varying(255)",
                maxLength: 255,
                nullable: true,
                oldClrType: typeof(string),
                oldType: "character varying(500)",
                oldMaxLength: 500,
                oldNullable: true);

            migrationBuilder.AlterColumn<int>(
                name: "FailedLoginAttempts",
                table: "AppUsers",
                type: "integer",
                nullable: false,
                oldClrType: typeof(int),
                oldType: "integer",
                oldDefaultValue: 0);

            migrationBuilder.AlterColumn<int>(
                name: "AccountStatus",
                table: "AppUsers",
                type: "integer",
                nullable: false,
                oldClrType: typeof(int),
                oldType: "integer",
                oldDefaultValue: 0);

            migrationBuilder.AddPrimaryKey(
                name: "PK_Vectors",
                table: "Vectors",
                column: "Id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_Sessions",
                table: "Sessions",
                column: "Id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_Roles",
                table: "Roles",
                column: "Id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_Permissions",
                table: "Permissions",
                column: "Id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_Documents",
                table: "Documents",
                column: "Id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_Conversations",
                table: "Conversations",
                column: "Id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_UserRoles",
                table: "UserRoles",
                columns: new[] { "UserId", "RoleId" });

            migrationBuilder.AddPrimaryKey(
                name: "PK_RolePermissions",
                table: "RolePermissions",
                columns: new[] { "RoleId", "PermissionId" });

            migrationBuilder.AddPrimaryKey(
                name: "PK_KnowledgeBases",
                table: "KnowledgeBases",
                column: "Id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_DocumentChunks",
                table: "DocumentChunks",
                column: "Id");

            migrationBuilder.AddPrimaryKey(
                name: "PK_AppUsers",
                table: "AppUsers",
                column: "Id");

            migrationBuilder.CreateIndex(
                name: "IX_Permissions_Code",
                table: "Permissions",
                column: "Code",
                unique: true);

            migrationBuilder.AddForeignKey(
                name: "FK_Conversations_AppUsers_UserId",
                table: "Conversations",
                column: "UserId",
                principalTable: "AppUsers",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_Conversations_Sessions_SessionId",
                table: "Conversations",
                column: "SessionId",
                principalTable: "Sessions",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_DocumentChunks_Documents_DocumentId",
                table: "DocumentChunks",
                column: "DocumentId",
                principalTable: "Documents",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_Documents_AppUsers_UserId",
                table: "Documents",
                column: "UserId",
                principalTable: "AppUsers",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_Documents_KnowledgeBases_KnowledgeBaseId",
                table: "Documents",
                column: "KnowledgeBaseId",
                principalTable: "KnowledgeBases",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_KnowledgeBases_AppUsers_UserId",
                table: "KnowledgeBases",
                column: "UserId",
                principalTable: "AppUsers",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_RolePermissions_Permissions_PermissionId",
                table: "RolePermissions",
                column: "PermissionId",
                principalTable: "Permissions",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_RolePermissions_Roles_RoleId",
                table: "RolePermissions",
                column: "RoleId",
                principalTable: "Roles",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_Sessions_AppUsers_UserId",
                table: "Sessions",
                column: "UserId",
                principalTable: "AppUsers",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_UserRoles_AppUsers_UserId",
                table: "UserRoles",
                column: "UserId",
                principalTable: "AppUsers",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_UserRoles_Roles_RoleId",
                table: "UserRoles",
                column: "RoleId",
                principalTable: "Roles",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);

            migrationBuilder.AddForeignKey(
                name: "FK_Vectors_DocumentChunks_ChunkId",
                table: "Vectors",
                column: "ChunkId",
                principalTable: "DocumentChunks",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }
    }
}
